package com.muyu.system.controller;

import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.system.domain.DmKeys;
import com.muyu.system.service.IDmKeysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 令牌信息Controller
 *
 * @author ruoyi
 * @date 2025-01-16
 */
@RestController
@RequestMapping("/keys")
public class DmKeysController extends BaseController {
    @Autowired
    private IDmKeysService dmKeysService;

    /**
     * 查询令牌信息列表
     */
    //@RequiresPermissions("system:keys:list")
    @GetMapping("/list")
    public Result list(DmKeys dmKeys) {
        startPage();
        List<DmKeys> list = dmKeysService.selectDmKeysList(dmKeys);
        return getDataTable(list);
    }

    /**
     * 导出令牌信息列表
     */
    //@RequiresPermissions("system:keys:export")
    @Log(title = "令牌信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DmKeys dmKeys) {
        List<DmKeys> list = dmKeysService.selectDmKeysList(dmKeys);
        ExcelUtil<DmKeys> util = new ExcelUtil<DmKeys>(DmKeys.class);
        util.exportExcel(response, list, "令牌信息数据");
    }

    /**
     * 获取令牌信息详细信息
     */
    //@RequiresPermissions("system:keys:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id) {
        return Result.success(dmKeysService.selectDmKeysById(id));
    }

    /**
     * 新增令牌信息
     */
    //@RequiresPermissions("system:keys:add")
    @Log(title = "令牌信息", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody DmKeys dmKeys) {
        int flag = dmKeysService.insertDmKeys(dmKeys);
        if (flag == 1) {
            return Result.success(flag,"新增成功");
        }else {
            return Result.error("新增失败");
        }

    }

    /**
     * 修改令牌信息
     */
    //@RequiresPermissions("system:keys:edit")
    @Log(title = "令牌信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody DmKeys dmKeys) {
        int flag = dmKeysService.updateDmKeys(dmKeys);
        if (flag == 1) {
            return Result.success("令牌绑定成功");
        } else {
            return Result.error("令牌无效");
        }

    }

    /**
     * 删除令牌信息
     */
    //@RequiresPermissions("system:keys:remove")
    @Log(title = "令牌信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids) {
        return Result.success(dmKeysService.deleteDmKeysByIds(ids));
    }
}
